﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install;

namespace ServiceMonitor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        private void CheckService(object sender, EventArgs e)
        {
            bool ifinstalled = false;
            ServiceController current = null;
            ServiceController[] servs = ServiceController.GetServices();
            foreach (ServiceController serv in servs)
            {
                if (serv.ServiceName.Equals("PGDownLoader"))
                {
                    current = serv;
                    ifinstalled = true;
                }
            }
            if (!ifinstalled)
            {
                try
                {
                    timer1.Stop();
                    string Path = AppDomain.CurrentDomain.BaseDirectory + "\\DownService.exe";
                    TransactedInstaller Tins = new TransactedInstaller();
                    //
                    AssemblyInstaller Ains = new AssemblyInstaller(Path, new string[] { });
                    Tins.Installers.Add(Ains);
                    Tins.Install(new System.Collections.Hashtable());
                    timer1.Start();
                }
                catch (Exception ex)
                {
                    lbStatus.Text = ex.Message;
                }
            }
            else
            {
                lbStatus.Text = current.Status.ToString();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
    }
}
